package cn.gok.oa.attendance.service.impl;

import cn.gok.oa.attendance.dto.AttendanceDto;
import cn.gok.oa.attendance.entity.TbAttendance;
import cn.gok.oa.attendance.mapper.TbAttendanceMapper;
import cn.gok.oa.attendance.service.ITbAttendanceService;
import cn.gok.oa.attendance.vo.AttendanceVo;
import cn.gok.oa.configuration.entity.TbConfiguration;
import cn.gok.oa.configuration.mapper.TbConfigurationMapper;
import cn.gok.oa.system.entity.TbUser;
import cn.gok.oa.system.mapper.TbDeptMapper;
import cn.gok.oa.system.mapper.TbUserDetailMapper;
import cn.gok.oa.system.mapper.TbUserMapper;
import cn.gok.oa.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@Service
public class TbAttendanceServiceImpl extends ServiceImpl<TbAttendanceMapper, TbAttendance> implements ITbAttendanceService {
    @Autowired
    TbAttendanceMapper attendanceMapper;
    @Autowired
    TbDeptMapper deptMapper;
    @Autowired
    TbUserMapper userMapper;
    @Autowired
    TbUserDetailMapper userDetailMapper;
    @Autowired
    TbConfigurationMapper configurationMapper;
    @Override
    public List<TbAttendance> queryByMonth(int userId,int month) {
        return attendanceMapper.queryByMonth(userId,month);
    }
//     类型为0时刷脸打卡，类型为1时定位打卡
    public int clockIn(TbAttendance attendance) throws ParseException {
        //获取配置表中的数据
        TbConfiguration configuration= configurationMapper.selectById(1);
//        if (attendance.getType() == 1) {
////            判断是否在范围内，不在范围内返回错误
//        }
//        刷脸打卡，返回员工图片
        LocalTime localTime = DateUtil.localDateTimeToLocalTime(DateUtil.dateToLocalDateTime(DateUtil.StringToDate(attendance.getTime())));
        //    状态为0考勤正常 状态为1迟到 ，状态为2缺勤
//        判断考勤时间,设置考勤状态
        if (localTime.compareTo(configuration.getStartTime()) > 0 && localTime.compareTo(configuration.getLateTime()) < 0){
            attendance.setAttendanceStatus(0);
        } else if (localTime.compareTo(configuration.getLateTime()) > 0 && localTime.compareTo(configuration.getAbsentTime())< 0) {
            attendance.setAttendanceStatus(1);
        }else {
            throw new RuntimeException("不在打卡时间范围内！");
        }
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUser::getId,attendance.getUserId());
        attendance.setDeptId(userMapper.selectOne(queryWrapper).getDeptId());
        if (attendanceMapper.insert(attendance) > 0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int signOut(TbAttendance attendance) throws ParseException {
        TbConfiguration configuration= configurationMapper.selectById(1);
        LocalTime localTime = DateUtil.localDateTimeToLocalTime(DateUtil.dateToLocalDateTime(DateUtil.StringToDate(attendance.getTime())));
        //    正常(4)，早退(3)插入
//        判断考勤时间,设置考勤状态
        if (localTime.compareTo(configuration.getEndTime()) > 0){
            attendance.setAttendanceStatus(4);
        } else if (localTime.compareTo(configuration.getEarlyTime()) > 0
                  && localTime.compareTo(configuration.getEndTime()) < 0) {
            attendance.setAttendanceStatus(3);
        } else {
            throw new RuntimeException();
        }
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUser::getId,attendance.getUserId());
        attendance.setDeptId(userMapper.selectOne(queryWrapper).getDeptId());
        if (attendanceMapper.insert(attendance) > 0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public List<AttendanceVo> getList(AttendanceDto dto) throws ParseException {
        Date startTime = null;
        Date endTime = null;
        if (dto.getDay() != null && dto.getDay()!=""){
            startTime =  DateUtil.StringToDate(dto.getYear() + "-" + dto.getMonth() + "-" + dto.getDay() + " 00:00:00");
            endTime = DateUtil.StringToDate(dto.getYear() + "-" + dto.getMonth() + "-" + dto.getDay() + " 23:59:59");
        //获取当月记录
        }else if(dto.getDay() == null || dto.getDay() == ""){
            LocalDate localDate = DateUtil.getMonthDay(dto.getYear(), dto.getMonth()).get(0);
            startTime =  DateUtil.LocalDateToDate(DateUtil.getMonthDay(dto.getYear(), dto.getMonth()).get(0));
            endTime = DateUtil.LocalDateToDate(DateUtil.getMonthDay(dto.getYear(), dto.getMonth()).get(1));

        }
        return attendanceMapper.getList(startTime,endTime,dto.getDeptName(),dto.getStatus());
    }

    @Override
    public List<AttendanceVo> Excel(){
        return attendanceMapper.excel();
    }
}
